/// Setting up the ANES File for presidential coalitions  

use "/Users/joshuazingher/Desktop/Prospectus /anes_cdfdta/anes coalitions .dta"

sort year

rename VCF0704 presvote

rename VCF0703 voted

rename VCF0130 churchie
rename VCF0131 churchie_2 
drop if ARep==0
rename AChurchie AChurchie2
rename Aweeklychurch AChurchie


/// Generating total churchie respondants by year

by year: egen Churchieresp = count(churchie) if churchie==1 | churchie==2 | churchie_2==1 

/// Generating total number of churchie voters by year


by year: egen churchievoted = count(voted) if churchie==1 & voted==3 | churchie==2 & voted==3 | churchie_2==1 & voted==3 

/// Generating total number of Latnio non-voters by year

by year: egen churchieNvoted = count(voted) if voted==0 & churchie==1 | voted==1 & churchie==1 | voted==2 & churchie==1 | voted==0 & churchie==2 | voted==1 & churchie==2 | voted==2 & churchie==2 | voted==0 & churchie_2==1 | voted==1 & churchie_2==1 | voted==2 & churchie_2==1


/// Generating total number of non-churchie voters by year

by year: egen Nchurchievoted = count(voted) if churchie==3 & voted==3 | churchie==4 & voted==3 | churchie==5 & voted==3  | churchie==7 & voted==3 | churchie_2==0 & voted==3  | churchie_2==2 & voted==3 | churchie_2==4 & voted==3 | churchie_2==5 & voted==3  | churchie==7 & voted==3 

/// Generating total number of non-churchie non-voters by year

by year: egen NchurchieNvoted = count(voted) if churchie==3 & voted==0 | churchie==3 & voted==1  | churchie==3 & voted==2 | churchie==4 & voted==0 | churchie==4 & voted==1  | churchie==4 & voted==2 | churchie==5 & voted==0 | churchie==5 & voted==1  | churchie==5 & voted==2 | churchie==7 & voted==0 | churchie==7 & voted==1  | churchie==7 & voted==2 | ///
churchie_2==0 & voted==0 | churchie_2==0 & voted==1  | churchie_2==0 & voted==2 | churchie_2==2 & voted==0 | churchie_2==2 & voted==1  | churchie_2==2 & voted==2 | churchie_2==4 & voted==0 | churchie_2==4 & voted==1  | churchie_2==5 & voted==2 | churchie_2==7 & voted==0 | churchie_2==7 & voted==1  | churchie_2==7 & voted==2 | churchie_2==4 & voted==0 | churchie_2==4 & voted==1  | churchie_2==4 & voted==2  



/// filling in the blanks

by year: egen Churchievoted = max(churchievoted)
by year: egen ChurchieNvoted = max(churchieNvoted)
by year: egen NChurchievoted = max(Nchurchievoted)
by year: egen NChurchieNvoted = max(NchurchieNvoted)




/// Generating % churchie TO with Mosteller Correction

gen N = 1
gen a = [Churchievoted * NChurchieNvoted] / [ChurchieNvoted * NChurchievoted]


///Generating filler vars

gen aminone =(a-1)
gen AChurchieplusTO = (AChurchie+aTO)
gen achurchieTO=(a*AChurchie*aTO)

gen CchurchieTO = 1/ (2*(aminone)) * (N+(aminone*AChurchieplusTO) - sqrt((N+aminone*AChurchieplusTO)*(N+aminone*AChurchieplusTO) - (4*(aminone*achurchieTO))))

///Adjusting TO for the formula

replace CchurchieTO = CchurchieTO/AChurchie



/// Moving to Partisanship - Generating churchie Democratic Vote total by year

by year: egen churchiedem = count(presvote) if presvote==2 & churchie==1 | presvote==2 & churchie==2 | presvote==2 & churchie_2==1

/// Generating churchie Non-Democratic Vote by Year

by year: egen churchieNdem = count(presvote) if presvote==1 & churchie==1 | presvote==1 & churchie==2 | presvote==1 & churchie_2==1

// Generating non-churchie Democratic Vote total by year

by year: egen Nchurchiedem = count(presvote) if presvote==2 & churchie==3 | presvote==2 & churchie==4 | presvote==2 & churchie==5 | presvote==2 & churchie==7 | presvote==2 & churchie_2==0 | presvote==2 & churchie_2==2 | presvote==2 & churchie_2==4 | presvote==2 & churchie_2==5 | presvote==2 & churchie_2==7 | presvote==2 & churchie_2==9

/// Generating non-churchie non-Democratic vote total by year

by year: egen NchurchieNdem = count(presvote) if presvote==1 & churchie==3 | presvote==1 & churchie==4 | presvote==1 & churchie==5 | presvote==1 & churchie==7 | presvote==1 & churchie_2==0 | presvote==1 & churchie_2==2 | presvote==1 & churchie_2==4 | presvote==1 & churchie_2==5 | presvote==1 & churchie_2==7 | presvote==1 & churchie_2==9




/// filling in the blanks

by year: egen ChurchieDem = max(churchiedem)
by year: egen ChurchieNDem = max(churchieNdem)
by year: egen NChurchieDem = max(Nchurchiedem)
by year: egen NChurchieNDem = max(NchurchieNdem)

/// Generation % churchie Democrat with Mosteller Correction

gen A = (ChurchieDem*NChurchieNDem)/(ChurchieNDem*NChurchieDem)
gen Aminone =(A-1)
gen AChurchieplusDem = (AChurchie+ARep)
gen AChurchieDem=(A*AChurchie*ARep)
gen CchurchieDem = 1/(2*(Aminone)) * (N+(Aminone*AChurchieplusDem) - sqrt((N+Aminone*AChurchieplusDem)*(N+Aminone*AChurchieplusDem) - (4*(Aminone*AChurchieDem))))

replace CchurchieDem = CchurchieDem/AChurchie


/// Generating number of ANES respondents by year

by year: egen totalresp = count (VCF0006a)


/// Coalition Contribution

gen Churchiecoalcontrib = (CchurchieTO* CchurchieDem *AChurchie)/(aTO*ARep)



